///  <copyright file="MallowMain.cs" company="Infozoom" author="Stefan Zschocke">
///    You must not remove this notice, or any other, from this software.
///    Otherwise do with it whatever you want.
///    infoZoom may not be made liable for any damages whatsoever 
///    (including, without limitation, damages for loss of business profits, 
///    business interruption, loss of business information or other pecuniary loss)
///    arising out of use of or inability to use the Software
/// </copyright>
using System;
using System.Text;
using System.Xml;

namespace Mallow
{
    /// <summary>
    /// Summary description for ProtocolBuilder. 
    /// </summary>
    internal class ProtocolBuilder
    {
        static string sChanged = "Changed   ";
        static string sAdded = "Added     ";
        static string sRemoved = "Removed   ";
        static string sUnchanged = "Unchanged ";
        int mComponentChanges = 0, mDirectoryChanges = 0, mFileChanges = 0;
        internal ProtocolBuilder() { }
        internal bool ComponentChangesDetected { get { return mComponentChanges != 0; } }

        internal void ComponentChanged(Component component)
        {
            mComponentChanges++;
            WriteComponentEntry(component, sChanged);
        }

        internal void ComponentRemoved(Component component)
        {
            mComponentChanges++;
            WriteComponentEntry(component, sRemoved);
        }

        internal void ComponentAdded(Component component)
        {
            mComponentChanges++;
            WriteComponentEntry(component, sAdded);
        }

        internal void ComponentUnchanged(Component component)
        {
            WriteComponentEntry(component, sUnchanged);
        }

        internal void FileRemoved(File file)
        {
            mFileChanges++;
            WriteFileEntry(file, sRemoved);
        }

        internal void FileAdded(File file)
        {
            mFileChanges++;
            WriteFileEntry(file, sAdded);
        }

        internal void DirectoryRemoved(Directory directory)
        {
            mDirectoryChanges++;
            WriteDirectoryEntry(directory, sRemoved);
        }

        internal void DirectoryAdded(Directory directory)
        {
            mDirectoryChanges++;
            WriteDirectoryEntry(directory, sAdded);
        }

        internal string GetProtocol(bool newFile)
        {
            StringBuilder protocol = new StringBuilder();

            protocol.Append("Mallow protocol " + System.DateTime.Now + Environment.NewLine);
            if (mComponentChanges == 0 && mFileChanges == 0 && mDirectoryChanges == 0)
            {
                protocol.Append(' ', mIndent);
                protocol.Append("No changes detected.");
            }
            else
            {
                protocol.Append(mComponentEntries);
                if (!newFile)
                {
                    protocol.Append(mDirectoryEntries);
                    protocol.Append(mFileEntries);
                }
            }
            return protocol.ToString();
        }

        private void WriteComponentEntry(Component c, string change)
        {
            mComponentEntries.Append(' ', mIndent);
            mComponentEntries.Append(change);
            mComponentEntries.Append("Component Id=");
            mComponentEntries.Append(IdOfNode(c.Node));
            mComponentEntries.Append(" Path=");
            mComponentEntries.Append(c.Directory.GetFullRelativeDirectoryName());
            mComponentEntries.Append(Environment.NewLine);
        }

        private void WriteDirectoryEntry(Directory c, string change)
        {
            mDirectoryEntries.Append(' ', mIndent);
            mDirectoryEntries.Append(change);
            mDirectoryEntries.Append("Directory Id=");
            mDirectoryEntries.Append(IdOfNode(c.Node));
            mDirectoryEntries.Append(" Path=");
            mDirectoryEntries.Append(c.GetFullRelativeDirectoryName());
            mDirectoryEntries.Append(Environment.NewLine);
        }

        private void WriteFileEntry(File c, string change)
        {
            mFileEntries.Append(' ', mIndent);
            mFileEntries.Append(change);
            mFileEntries.Append("File Id=");
            mFileEntries.Append(IdOfNode(c.Node));
            mFileEntries.Append(' ');
            mFileEntries.Append("Component=");
            mFileEntries.Append(IdOfNode(c.Component.Node));
            mFileEntries.Append(" Path=");
            mFileEntries.Append(c.GetFullRelativePath());
            mFileEntries.Append(Environment.NewLine);
        }

        private string IdOfNode(XmlNode node)
        {
            XmlAttribute a = node.Attributes["Id"];
            if (a == null) return "(not found)";
            return a.Value;
        }

        int mIndent = 2;
        StringBuilder mComponentEntries = new StringBuilder();
        StringBuilder mDirectoryEntries = new StringBuilder();
        StringBuilder mFileEntries = new StringBuilder();
    }
}
